package org.example.course.controller;

import org.example.course.common.DataGridView;
import org.example.course.common.ResultObj;
import org.example.course.common.utils.StringUtils;
import org.example.course.domain.User;
import org.example.course.dto.UserModel;
import org.example.course.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import static org.example.course.common.utils.PinyinUtils.getPingYin;

/**
 * @Classname UserController
 * @Description 用户管理
 * @Date 2020/5/8 14:02
 * @Created by
 */
@RestController
@RequestMapping("users")
public class UserController {


    @Autowired
    private UserService service;


    /**
     * 分页查询  可带参 按条件查询
     *
     * @param userModel
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public DataGridView page(UserModel userModel) {
        DataGridView dataGridView = service.findPage(userModel);
        return dataGridView;
    }

    /**
     * 修改密码
     *
     * @param oldPassword
     * @param password
     * @param repassword
     * @return
     */
    @RequestMapping(value = "/password", method = RequestMethod.POST)
    public ResultObj password(String oldPassword, String password, String repassword) {
        if (service.updateUserPwd(oldPassword, password)) {
            return ResultObj.OPERATE_SUCCESS;
        }
        return ResultObj.OPERATE_ERROR;
    }

    /**
     * 创建用户
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultObj create(User user) {
        if (user == null) {
            return ResultObj.CREATE_ERROR;
        }
        if (service.create(user)) {
            return ResultObj.CREATE_SUCCESS;
        }
        return ResultObj.CREATE_ERROR;
    }


    /**
     * 更新用户
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultObj update(User user) {
        if (user == null) {
            return ResultObj.UPDATE_ERROR;
        }

        if (service.update(user)) {
            return ResultObj.UPDATE_SUCCESS;
        }
        return ResultObj.UPDATE_ERROR;
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultObj delete(@PathVariable String id) {
        if (!StringUtils.isNumeric(id)) {
            return ResultObj.COMMON_WARN_ID_SHOULD_INTEGER;
        }
        if (service.delete(id)) {
            return ResultObj.DELETE_SUCCESS;
        }
        return ResultObj.DELETE_ERROR;
    }


    /**
     * 重置用户密码
     *
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/resetPassword/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultObj resetPassword(@PathVariable String id) throws Exception {
        if (!StringUtils.isNumeric(id)) {
            return ResultObj.COMMON_WARN_ID_SHOULD_INTEGER;
        }
        if (service.resetPassword(id)) {
            return ResultObj.RESET_SUCCESS;
        }
        return ResultObj.RESET_ERROR;
    }


    /**
     * 汉字转拼音
     *
     * @param userName
     * @return
     */
    @RequestMapping(value = "/changChineseToPinyin", method = RequestMethod.GET)
    public Map<String, Object> changChineseToPinyin(String userName) {
        Map<String, Object> map = new HashMap<>();
        if (null != userName) {
            map.put("value", getPingYin(userName));
        } else {
            map.put("value", "");
        }
        return map;
    }
}
